package com.ibeetl.admin.api.web;
import com.ibeetl.admin.api.entity.Travel_guide;
import com.ibeetl.admin.api.service.TravelGuideService;
import com.ibeetl.admin.api.web.query.TravelGuideQuery;
import com.ibeetl.admin.core.web.JsonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.page.PageResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@Tag(
        name = "TravelGuideController")
public class TravelGuideController {
    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/api/travel_guide";
    private static final String MODEL_WEB = "/web/travel_guide";
    @Resource
    private TravelGuideService travelGuideService;

    @Operation(
            summary = "新增导游信息"
    )
    @PostMapping(MODEL + "/add.json")
    @CrossOrigin
    public JsonResult<Travel_guide> add(Travel_guide travelGuide) {
        travelGuide.setCreateDt(new Date());
        travelGuide.setIsValid(1);//未审核
        if(travelGuideService.save(travelGuide)){
            Long guideId = travelGuideService.queryNewIdByUserId(travelGuide.getUserId());
            travelGuide.setGuideId(guideId);
            return JsonResult.success(travelGuide);
        }else{
            return JsonResult.failMessage("新增失败！");
        }
    }
    @Operation(
            summary = "更新导游信息"
    )
    @PostMapping(MODEL + "/update.json")
    @CrossOrigin
    public JsonResult<Travel_guide> update(Travel_guide travelGuide)  {

        travelGuideService.update(travelGuide);
        return JsonResult.success(travelGuide);
    }

    @PostMapping(MODEL_WEB + "/audit.json")
    @CrossOrigin
    public JsonResult<Travel_guide> audit(Travel_guide travelGuide)  {
        travelGuideService.audit(travelGuide);
        return JsonResult.success(travelGuide);
    }

    @Operation(
            summary = "根据状态获取导游列表"
    )
    @PostMapping(MODEL + "/queryByState.json")
    @CrossOrigin
    public JsonResult<List> queryByState(@RequestParam(required = false) Long userId, Integer isValid) {
        List<Travel_guide>  travel_guides  = travelGuideService.queryByState(userId,isValid);
        return JsonResult.success(travel_guides);
    }

    @Operation(
            summary = "根据导游编号获取明细信息"
    )
    @PostMapping(MODEL + "/queryById.json")
    @CrossOrigin
    public JsonResult<Travel_guide> queryById(Long guideId) {
        Travel_guide travel_guide  = travelGuideService.queryById(guideId);
        return JsonResult.success(travel_guide);
    }

    @PostMapping(MODEL_WEB + "/list.json")
    @ResponseBody
    public JsonResult<PageResult<Travel_guide>> list(TravelGuideQuery condition)
    {
        PageResult<Travel_guide> pageResult = travelGuideService.queryByCondition(condition);
        return JsonResult.success(pageResult);
    }
}
